package cn.yujian95.hospital.service.impl;

import cn.yujian95.hospital.dto.param.HospitalProductParam;
import cn.yujian95.hospital.entity.HospitalProduct;
import cn.yujian95.hospital.mapper.HospitalProductMapper;
import cn.yujian95.hospital.service.IHospitalProductService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class HospitalProductServiceImpl extends ServiceImpl<HospitalProductMapper, HospitalProduct> implements IHospitalProductService {

    @Resource
    private HospitalProductMapper hospitalProductMapper;

    /**
     * 判断是否存在名称
     *
     * @param name
     * @return
     */
    @Override
    public boolean count(String name) {
        LambdaQueryWrapper<HospitalProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HospitalProduct::getName, name);
        HospitalProduct one = hospitalProductMapper.selectOne(wrapper);
        return one != null;
    }

    /**
     * 判断是否存在这个产品
     *
     * @param id
     * @return
     */
    @Override
    public boolean count(Long id) {
        HospitalProduct product = getById(id);
        return product != null;
    }


    @Override
    public boolean insertProduct(HospitalProductParam param) {
        HospitalProduct hospitalProduct = new HospitalProduct();
        BeanUtils.copyProperties(param, hospitalProduct);
        return save(hospitalProduct);
    }

    /**
     * 修改产品信息
     *
     * @param id
     * @param param
     * @return
     */
    @Override
    public boolean update(Long id, HospitalProductParam param) {
        HospitalProduct hospitalProduct = new HospitalProduct();
        BeanUtils.copyProperties(param, hospitalProduct);
        hospitalProduct.setId(id);
        return updateById(hospitalProduct);
    }

    /**
     * 删除产品
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        if (count(id)) {
            return removeById(id);
        }
        return false;
    }

    /**
     * 获取产品信息
     *
     * @param id
     * @return
     */
    @Override
    public HospitalProduct getProduct(Long id) {
        if (count(id)) {
            return getById(id);
        }
        return null;
    }

    /**
     * 获取产品列表（分页查询）
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<HospitalProduct> list(String name, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<HospitalProduct> wrapper = new LambdaQueryWrapper<>();
        IPage<HospitalProduct> page = new Page<>(pageNum, pageSize);

        if (name != null) {
            wrapper.like(HospitalProduct::getName, name);
        }
        return page(page, wrapper);
    }

    /**
     * 获取产品列表（分页查询）小程序端搜索去除下架
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<HospitalProduct> listForApp(String name, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<HospitalProduct> wrapper = new LambdaQueryWrapper<>();
        IPage<HospitalProduct> page = new Page<>(pageNum, pageSize);

        if (name != null) {
            wrapper.like(HospitalProduct::getName, name);
        }

        // 今天的日期
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());


        IPage<HospitalProduct> productIPage = page(page, wrapper);
        List<HospitalProduct> records = productIPage.getRecords();

        if (records != null) {
            List<HospitalProduct> collect = records.stream().filter(item -> {
                // 0为上架的
                return "0".equals(item.getUp()) && format.compareTo(item.getStart()) >= 0 && format.compareTo(item.getEnd()) <= 0;
            }).collect(Collectors.toList());

            productIPage.setRecords(collect);
            productIPage.setTotal(collect.size());
            return productIPage;
        }
        return productIPage;
    }

    /**
     * 获取产品列表（分页查询）
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<HospitalProduct> listForAppList(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<HospitalProduct> wrapper = new LambdaQueryWrapper<>();
        IPage<HospitalProduct> page = new Page<>(pageNum, pageSize);
        // 今天的日期
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        IPage<HospitalProduct> productIPage = page(page, wrapper);
        List<HospitalProduct> records = productIPage.getRecords();
        if (records != null) {
            records = records.stream().filter(item -> {
                return "0".equals(item.getUp()) && format.compareTo(item.getStart()) >= 0 && format.compareTo(item.getEnd()) <= 0;
            }).collect(Collectors.toList());
            page.setRecords(records);
            page.setTotal(records.size());
            return page;
        }
        return page(page, wrapper);
    }

}
